const birdDom = document.querySelector('.bird')
const birdStyles = getComputedStyle(birdDom)
const birdWidth = parseFloat(birdStyles.width)
const birdHeight = parseFloat(birdStyles.height)
const birdLeft = parseFloat(birdStyles.left)
const birdTop = parseFloat(birdStyles.top)
const gameDom = document.querySelector('.game')
const gameHeight = parseFloat(gameDom.clientHeight)

class Bird extends Rectangle {
    constructor() {
        super(birdWidth, birdHeight, birdLeft, birdTop, 0, 0, birdDom)
        // 加速度
        this.g = 1500
        this.timer = null
        this.swingStatus = 1
        this.render() // 这里执行初始化
    }

    render() {
        super.render()
        this.dom.className = `bird swing${this.swingStatus}`
    }

    onMove() {
        this.maxY = gameHeight - landHeight - birdHeight
        // console.log(this.maxY, this.top)
        // 限定最低着陆点
        if (this.top >= this.maxY) this.top = this.maxY
        // 限定最高飞行点
        // if (this.top <= 0) this.top = 0
    }

    startSwing() {
        if (this.timer) return
        this.timer = setInterval(() => {
            this.swingStatus++
            this.swingStatus = this.swingStatus % 3 + 1
            this.render()
        }, 300)
    }

    stopSwing() {
        clearInterval(this.timer)
        this.timer = null
    }

    move(duration) {
        super.move(duration)
        // 根据加速度改变速度
        this.ySpeed += this.g * duration
    }

    jump() {
        this.ySpeed = -450
    }
}

const b1 = new Bird()
setInterval(() => {
    b1.move(16 / 1000)
}, 16)